package com.ajie.slimming.ui;

import java.io.File;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

import com.ajie.slimming.util.Util;

public class SlimmingApplication extends Application
{
    /** 软件根目录 */
    private static String rootDir;
    
    /** 软件图片目录(暂无) */
    private static String imageDir;
    
    /** 软件录音目录(暂无) */
    private static String audioDir;
    
    /** 软件历史记录目录 */
    private static String historyDir;
    
    private static Context mAppContext;
    
    /** 版本号 */
    private static int versionCode = 0;
    
    /** 版本名称 */
    private static String versionName = "";
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        mAppContext = this.getApplicationContext();
        //DBApi.getInstance();
        
        try
        {
            versionCode = mAppContext.getPackageManager()
                    .getPackageInfo("com.ajie.slimming", 0).versionCode;
            versionName = mAppContext.getPackageManager()
                    .getPackageInfo("com.ajie.slimming", 0).versionName;
        }
        catch (NameNotFoundException e)
        {
            e.printStackTrace();
        }
        
        init();
    }
    
    public static Context getBPContext()
    {
        return mAppContext;
    }
    
    public static int getVersionCode()
    {
        return versionCode;
    }
    
    public static String getVersionName()
    {
        return versionName;
    }
    
    public static String getVersion()
    {
        return versionName + "(" + versionCode + ")";
    }
    
    private void init()
    {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        {
            rootDir = Environment.getExternalStorageDirectory()
                    .getAbsolutePath();
        }
        else
        {
            File internalCacheDir = getCacheDir();
            rootDir = internalCacheDir.getAbsolutePath();
        }
        rootDir = rootDir + "/slimming/";
        imageDir = rootDir + "image";
        audioDir = rootDir + "audio";
        historyDir = rootDir + "history";
        Util.createFileDir(rootDir);
        Util.createFileDir(imageDir);
        Util.createFileDir(audioDir);
        Util.createFileDir(historyDir);
    }
    
    /**
     * 返回根目录
     * 
     * @return
     */
    public static String getRootDir()
    {
        return rootDir;
    }
    
    /**
     * 返回图片目录
     * 
     * @return
     */
    public static String getImageRootDir()
    {
        return imageDir;
    }
    
    /**
     * 返回音频目录
     * 
     * @return
     */
    public static String getAudioRootDir()
    {
        return audioDir;
    }
    
    /**
     * 返回历史记录目录
     * 
     * @return
     */
    public static String getHistoryRootDir()
    {
        return historyDir;
    }
}
